package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.ACCOUNTBean;
import com.kpx.app.bean.CertificationInfoBean;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.cache.FileCache;
import com.kpx.app.configs.BroadcastFilters;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.ImageLoader;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.view.ViewCertification;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by congdy on 17/3/3.
 * 我的认证
 */

public class ActivityCertification extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.img_user_icon)
    ImageView imgUserIcon;
    @BindView(R.id.ll_certification_content)
    LinearLayout llCertificationContent;
    private CertificationInfoBean certificationInfoBean;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_certification);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "认证");
    }

    /**
     * 设置认证信息
     */
    private void setCertificationItem(LoginUser loginUser) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (this.isDestroyed()) {
                return;
            }
        }
        llCertificationContent.removeAllViews();
        if (!TextUtils.isEmpty(loginUser.getUSER().getHeadImg())) {
            ImageLoader.loadCircle(ActivityCertification.this, loginUser.getUSER().getHeadImg(), imgUserIcon, R.drawable.default_head_img);
        }
        int type = UserInfoUtils.getCachedLoginUser().getUSER().getSteps() == 0 ? 1 : UserInfoUtils.getCachedLoginUser().getUSER().getSteps();
        for (int i = 0; i < 5; i++) {
            ViewCertification v = new ViewCertification(ActivityCertification.this);
            v.setTag(i);
            v.setMention(i);
            if (i < type) {
                v.setStatus(ViewCertification.STATUS_PASS, true);
            } else if (i == type) {
                v.setStatus(ViewCertification.STATUS_TOCERTIFICATION, true);
            } else {
                v.setStatus(ViewCertification.STATUS_TOCERTIFICATION, false);
            }
            llCertificationContent.addView(v);
        }

    }

    @Override
    public void initData() {

    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
        if(loginUser==null||(loginUser!=null&&loginUser.getUSER()==null)){
            return;
        }
        setCertificationItem(UserInfoUtils.getCachedLoginUser());
        final int temstep = loginUser.getUSER().getSteps();
        UserInfoUtils.getNetLoginUser(ActivityCertification.this, new UserInfoUtils.OnGetNetInfoBack() {
            @Override
            public void back(final LoginUser info) {
                if (info != null) {
                    if (info.getUSER().getSteps() >= 2 && info.getUSER().getSteps() <= 4) {
                        if(temstep<=2){
                            sendBroadcast(new Intent(BroadcastFilters.BROADCAST_ACTION_REFRESH_TASK_DATA));
                        }
                        showLoadingDialog();
                        MyHashMap map = new MyHashMap();
                        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
                            @Override
                            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                                dissmissLoadingDialog();
                                if (result.getCode() == 0) {
                                    CertificationInfoBean b = (CertificationInfoBean) result.getData();
                                    if (b != null) {
                                        certificationInfoBean = b;
                                        if (certificationInfoBean != null) {
                                            LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
                                            loginUser.setACCOUNT((ArrayList<ACCOUNTBean>) certificationInfoBean.getUserThirdAccount());
                                            UserInfoUtils.saveLoginUser(loginUser);
                                            FileCache.sharedInstance().put(Constants.KEY_CERTIFICATION_BEAN, certificationInfoBean);
                                        }
                                        setCertificationItem(info);
                                        for (int i = 0; i < llCertificationContent.getChildCount(); i++) {
                                            ((ViewCertification) llCertificationContent.getChildAt(i)).setCerInfo(b);
                                            if (info.getUSER().getSteps() == 2 && ((Integer) llCertificationContent.getChildAt(i).getTag() == 2)) {
                                                if (b.getPhotosAuthenStatus() == 2) {//审核中
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_WAITTING, true);
                                                    ((ViewCertification) llCertificationContent.getChildAt(i + 1)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, false);
                                                    info.setSteps(info.getSteps() - 1);
                                                } else if (b.getPhotosAuthenStatus() == 0) {//需要认证
                                                    ((ViewCertification) llCertificationContent.getChildAt(i + 1)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, false);
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, true);
                                                } else if (b.getPhotosAuthenStatus() == 1) {//认证通过
                                                    ((ViewCertification) llCertificationContent.getChildAt(i + 1)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, true);
                                                    ((ViewCertification) llCertificationContent.getChildAt(i + 2)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, true);
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_PASS, true);
                                                } else if (b.getPhotosAuthenStatus() == 3) {//审核失败
                                                    ((ViewCertification) llCertificationContent.getChildAt(i + 1)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, false);
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_FAIL, true);
                                                }
                                            }
                                            if ((info.getUSER().getSteps() == 3) && ((Integer) llCertificationContent.getChildAt(i).getTag() == 3)) {
                                                ((ViewCertification) llCertificationContent.getChildAt(i + 1)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, true);
                                                if (b.getVideosAuthenStatus() == 2) {
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_WAITTING, true);
                                                    info.setSteps(info.getSteps() - 1);
                                                } else if (b.getVideosAuthenStatus() == 0) {//需要认证
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, true);
                                                } else if (b.getVideosAuthenStatus() == 1) {//认证通过
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_PASS, true);
                                                } else if (b.getVideosAuthenStatus() == 3) {//审核失败
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_FAIL, true);
                                                }
                                            }
                                            if((info.getUSER().getSteps() == 4) && ((Integer) llCertificationContent.getChildAt(i).getTag() == 4)){
                                                if (b.getThirdAccountStatus() == 0) {//立即
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_TOCERTIFICATION, true);
                                                }else if(b.getThirdAccountStatus() == 1){//完成
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_PASS, true);
                                                }else if(b.getThirdAccountStatus() == 2){//继续
                                                    ((ViewCertification) llCertificationContent.getChildAt(i)).setStatus(ViewCertification.STATUS_WAITTING, true);
                                                }
                                            }
                                        }


                                    }
                                } else {
                                    ToastUtil.getInstance().showToast(ActivityCertification.this, result.getMsg());
                                }
                            }

                            @Override
                            public void onRequestError(String url, int statusCode, String error) {
                                dissmissLoadingDialog();
                                ToastUtil.getInstance().showToast(ActivityCertification.this, error);
                            }

                        }, CertificationInfoBean.class, HttpDataRequest.TYPE_OBJECT).POST(HttpDataReqUrl.URL_POST_CHECK_CERTIFICATION, true, map);
                    } else {
                        setCertificationItem(info);
                    }
                }
            }
        });

    }

}
